package com.ama.utils.urltoImage;

import java.io.*;
import java.util.regex.Pattern;

public class FileToolsUtil {
	
	/**
	 * 将content写入file，默认是utf-8编码，覆盖写
	 * @param path    路径
	 * @param name    名称，带后缀
	 * @param content 内容
	 * @return 1        正常
	 *         0   UnsupportedEncodingException
	 *        -1   FileNotFoundException
	 *        -2   IOException
	 */
	public static int string2file(String path, String name, String content) {
		return string2file(path, name, content, "utf-8");
	}
	
	/**
	 * 
	 * ��contentд��file�У�Ĭ�ϸ��Ƿ�ʽд�ļ�
	 * @param path        ���file��·��
	 * @param name        ���file����ƣ����׺
	 * @param content     file ����
	 * @param charsetName ���file�ı�����?
	 * @return 1         �ɹ����?
	 *         0   UnsupportedEncodingException
	 *        -1   FileNotFoundException
	 *        -2   IOException
	 */
	public static int string2file(String path, String name, String content, String charsetName) {
		return string2file(path, name, content, charsetName, false);
	}
	
	/**
	 * 
	 * ��contentд��file��
	 * @param path        ���file��·��
	 * @param name        ���file����ƣ����׺
	 * @param content     file ����
	 * @param charsetName ���file�ı�����?
	 * @param append      д�ļ�ʱ�Ƿ�׷�ӣ�true׷�ӵ��ļ�β��falseΪ����
	 * @return 1         �ɹ����?
	 *         0   UnsupportedEncodingException
	 *        -1   FileNotFoundException
	 *        -2   IOException
	 */
	public static int string2file(String path, String name, String content, String charsetName, boolean append) {
		int flag = 1;
		String filename = path + name;
		
		try {
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename, append), charsetName));
			bw.write(content);
			bw.close();
			flag = 1;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			flag = 0;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			flag = -1;
		} catch (IOException e) {
			e.printStackTrace();
			flag = -2;
		}
		
		return flag;
	}
	
	
	/**
	 * ��ȡfile���ݵ�string, file ������?gbk
	 * 
	 * @param file
	 * @return
	 */
	public static String file2string(File file) {

		return file2string(file, "gbk");
	}
	
	/**
	 * ��ȡfile���ݵ�string, file �����?
	 * 
	 * @param file
	 * @return
	 */
	public static String file2string(File file, String charset) {

		// Ҫ���ص�string
		String output = "";
		
		try {
			BufferedReader input = new BufferedReader(new InputStreamReader(
					new FileInputStream(file), charset));
			StringBuffer buffer = new StringBuffer();
			int oneChar;

			while ((oneChar = input.read()) != -1) {
				buffer.append((char)oneChar);
			}
				
			output = buffer.toString();

			input.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return output;
	}
	
	
	/**
	 * ��ȡfile���ݵ�string
	 * 
	 * @param file
	 * @param charset  file ������?
	 * @return
	 */
	public static String file2string(String path, String charset) {

		// Ҫ���ص�string
		String output = "";
		File file = new File(path);
		
		if (file.exists() == false) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		try {
			BufferedReader input = new BufferedReader(new InputStreamReader(
					new FileInputStream(file), charset));
			StringBuffer buffer = new StringBuffer();
			int oneChar;

			while ((oneChar = input.read()) != -1) {
				buffer.append((char)oneChar);
			}

			output = buffer.toString();

			input.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return output;
	}
	
	
	/**
	 * �ƶ��ļ�
	 * @param _srcFile      Դ�ļ���ȫ·��+���?
	 * @param _dstFile      Ŀ���ļ���ȫ·��+���?
	 * @param srcCharset   Դ�ļ��ı��룬�û�����
	 * @param dstCharset   Ŀ���ļ����� ���û�ָ��
	 * @return  1   �ɹ�
	 *          0   �����쳣
	 */
	public static int moveFile(String _srcFile, String _dstFile, String srcCharset, String dstCharset) {
		int flag = 1;
		
		File srcFile = new File(_srcFile);

		try {
			
			// ������
			BufferedReader input = new BufferedReader(new InputStreamReader(
					new FileInputStream(srcFile), srcCharset));
			
			// �����?
			BufferedWriter output = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(_dstFile, true), dstCharset));
			
			// ÿ�θ���һ�����?
			String oneLine;
			while ((oneLine = input.readLine()) != null) {
				output.write(oneLine + "\n");
			}
			
			input.close();
			output.close();

		} catch (IOException e) {
			e.printStackTrace();
			flag = 0;
		}
		
		// ɾ��Դ�ļ�
		srcFile.delete();
		
		return flag;
	}
	
	/**
	 * 将str中的非法文件名字符替换成空格
	 * @param str
	 * @return
	 */
	public static String toValidfilename(String str) {
		Pattern filePattern = Pattern.compile("[\\\\/:*?\"<>|]");
		String ret = "";
		
		if (str == null) {
			System.err.println("filename is null...");
			return "";
		} else {
			ret = filePattern.matcher(str).replaceAll(" ");
		}
		return ret;
	}

	
	
	public static void main(String[] args) {
//		System.out.println(FileToolsUtil.moveFile("d:\\response.xml", "d:\\log\\resp.xml", "gbk", "utf-8"));
		System.out.println(FileToolsUtil.file2string("d:\\1111.txt", "gbk"));
	}

}
